package com.training.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.training.R;

public class CircularProgressView extends View {
    // 进度值
    private float progress = 0f;
    // 最大进度值
    private float maxProgress = 100f;
    // 进度条颜色
    private int progressColor = Color.BLUE;
    // 背景圆环颜色
    private int backgroundColor = Color.GRAY;
    // 圆环宽度
    private float ringWidth = 8f;
    // 文字颜色
    private int textColor = Color.BLACK;
    // 文字大小
    private float textSize = 14f;
    // 是否显示百分比符号
    private boolean showPercentSign = true;
    // 绘制进度条的画笔
    private Paint progressPaint;
    // 绘制背景圆环的画笔
    private Paint backgroundPaint;
    // 绘制文字的画笔
    private Paint textPaint;
    // 圆形区域
    private RectF rectF;

    public CircularProgressView(Context context) {
        super(context);
        init();
    }

    public CircularProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        init();
    }

    public CircularProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        init();
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.CircularProgressView);
        
        progress = typedArray.getFloat(R.styleable.CircularProgressView_progress, 0f);
        maxProgress = typedArray.getFloat(R.styleable.CircularProgressView_maxProgress, 100f);
        progressColor = typedArray.getColor(R.styleable.CircularProgressView_progressColor, Color.BLUE);
        backgroundColor = typedArray.getColor(R.styleable.CircularProgressView_backgroundColor, Color.GRAY);
        ringWidth = typedArray.getDimension(R.styleable.CircularProgressView_ringWidth, 8f);
        textColor = typedArray.getColor(R.styleable.CircularProgressView_textColor, Color.BLACK);
        textSize = typedArray.getDimension(R.styleable.CircularProgressView_textSize, 14f);
        showPercentSign = typedArray.getBoolean(R.styleable.CircularProgressView_showPercentSign, true);
        
        typedArray.recycle();
    }

    private void init() {
        // 初始化画笔
        progressPaint = new Paint();
        progressPaint.setAntiAlias(true);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setColor(progressColor);
        
        backgroundPaint = new Paint();
        backgroundPaint.setAntiAlias(true);
        backgroundPaint.setStyle(Paint.Style.STROKE);
        backgroundPaint.setStrokeCap(Paint.Cap.ROUND);
        backgroundPaint.setColor(backgroundColor);
        
        textPaint = new Paint();
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(textSize);
        textPaint.setColor(textColor);
        textPaint.setTextAlign(Paint.Align.CENTER);
        
        rectF = new RectF();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        
        int size = Math.min(width, height);
        setMeasuredDimension(size, size);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        
        // 计算圆的半径
        int radius = getWidth() / 2 - (int)(ringWidth / 2);
        
        // 设置矩形区域
        rectF.set((getWidth() - radius * 2) / 2, (getHeight() - radius * 2) / 2, (getWidth() + radius * 2) / 2, (getHeight() + radius * 2) / 2);
        
        // 绘制背景圆环
        backgroundPaint.setStrokeWidth(ringWidth);
        canvas.drawArc(rectF, -90, 360, false, backgroundPaint);
        
        // 绘制进度条
        progressPaint.setStrokeWidth(ringWidth);
        float sweepAngle = (progress / maxProgress) * 360;
        canvas.drawArc(rectF, -90, sweepAngle, false, progressPaint);
        
        // 绘制文字
        String text = progress + (showPercentSign ? "%" : "");
        float textWidth = textPaint.measureText(text);
        float x = getWidth() / 2;
        float y = getHeight() / 2 + textPaint.getFontMetrics().descent / 2;
        canvas.drawText(text, x, y, textPaint);
    }

    // Getter和Setter方法
    public float getProgress() {
        return progress;
    }

    public void setProgress(float progress) {
        if (progress < 0) {
            progress = 0;
        }
        if (progress > maxProgress) {
            progress = maxProgress;
        }
        this.progress = progress;
        invalidate();
    }

    public float getMaxProgress() {
        return maxProgress;
    }

    public void setMaxProgress(float maxProgress) {
        this.maxProgress = maxProgress;
        invalidate();
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        progressPaint.setColor(progressColor);
        invalidate();
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        backgroundPaint.setColor(backgroundColor);
        invalidate();
    }

    public float getRingWidth() {
        return ringWidth;
    }

    public void setRingWidth(float ringWidth) {
        this.ringWidth = ringWidth;
        invalidate();
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
        textPaint.setColor(textColor);
        invalidate();
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
        textPaint.setTextSize(textSize);
        invalidate();
    }

    public boolean isShowPercentSign() {
        return showPercentSign;
    }

    public void setShowPercentSign(boolean showPercentSign) {
        this.showPercentSign = showPercentSign;
        invalidate();
    }
}